#include "stdafx.h"

/****************************************
* StatusBar
***/
void StatusBar::Create(HWND HwndOwner, HMENU ID)
{

	// Ensure that the common control DLL is loaded.
	InitCommonControls();

	// Create the status bar.
	hwnd = CreateWindowEx(
		0,                       // no extended styles
		STATUSCLASSNAME,         // name of status bar class
		(LPCTSTR) NULL,          // no text when first created
		SBARS_SIZEGRIP |         // includes a sizing grip
		WS_CHILD | WS_VISIBLE,   // creates a visible child window
		0, 0, 0, 0,              // ignores size and position
		HwndOwner,              // handle to parent window
		(HMENU) ID,       // child window identifier
		Application::hInst,                   // handle to application instance
		NULL);                   // no window creation data

	//Init the nParts and lParts
	int nParts = 1;
	int lpParts[1] = {-1};
	SetParts(nParts, lpParts);
}

void StatusBar::SetParts(int nParts, LPINT lpParts)
{
	SendMessage(hwnd, SB_SETPARTS, (WPARAM) nParts, (LPARAM)lpParts);
}

void StatusBar::SetText(int iPart, PTSTR szText, WPARAM uType)
{
	SendMessage(hwnd, SB_SETTEXT, iPart | uType, (LPARAM) szText);
}

int StatusBar::GetHeight()
{
	RECT rect;
	::GetWindowRect(hwnd, &rect);
	return rect.bottom - rect.top;
}

//---------------------------------------

HWND Controls::hwndEditBar;
HWND Controls::hwndEditBar2;
HWND Controls::hwndToolBar;

/* ReBar */
HWND WINAPI Controls::CreateRebar(HWND hwndOwner)
{
	REBARINFO     rbi;
	REBARBANDINFO rbBand;
	RECT          rc;
	HWND   hwndRB, hwndTB;
	DWORD  dwBtnSize;
	INITCOMMONCONTROLSEX icex;

	icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
	icex.dwICC   = ICC_COOL_CLASSES|ICC_BAR_CLASSES;
	InitCommonControlsEx(&icex);
	hwndRB = CreateWindowEx(WS_EX_TOOLWINDOW,
		REBARCLASSNAME,
		NULL,
		WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|
		WS_CLIPCHILDREN|RBS_VARHEIGHT|RBS_FIXEDORDER |
		CCS_NODIVIDER,
		0,0,0,0,
		hwndOwner,
		NULL,
		Application::hInst,
		NULL);

	if(!hwndRB)
		return NULL;

	// Initialize and send the REBARINFO structure.
	rbi.cbSize = sizeof(REBARINFO);  // Required when using this
	// structure.
	rbi.fMask  = 0;
	rbi.himl   = (HIMAGELIST)NULL;
	if(!SendMessage(hwndRB, RB_SETBARINFO, 0, (LPARAM)&rbi))
		return NULL;
	// Initialize structure members that both bands will share.
	rbBand.cbSize = sizeof(REBARBANDINFO);  // Required
	rbBand.fMask  = RBBIM_COLORS | RBBIM_BACKGROUND | 
		RBBIM_STYLE | RBBIM_CHILD | RBBIM_TEXT | RBBIM_CHILDSIZE | 
		RBBIM_SIZE;
	rbBand.fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP | RBBS_BREAK | RBBS_NOGRIPPER;
	rbBand.hbmBack = LoadBitmap(Application::hInst,
		MAKEINTRESOURCE(IDB_BACKGRND));   

	//----ToolBar-------
	hwndTB = CreateToolBar(hwndOwner);

	// Get the height of the toolbar.
	dwBtnSize = SendMessage(hwndTB, TB_GETBUTTONSIZE, 0,0);

	// Set values unique to the band with the toolbar.
	rbBand.lpText     = TEXT("");
	rbBand.hwndChild  = hwndTB;
	rbBand.cxMinChild = 10 * HIWORD(dwBtnSize);
	rbBand.cyMinChild = LOWORD(dwBtnSize);
	rbBand.cx         = 0;

	// Add the band that has the toolbar.
	SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);

	//--------------------------
	int cxEdit1 = 100, cyEdit = 20;
	//----EditBar1---

	hwndEditBar = CreateWindowEx(0L, L"Edit", NULL, WS_CHILD | WS_BORDER
		| WS_VISIBLE | ES_CENTER  | ES_AUTOVSCROLL | ES_READONLY, 
		0, 0, cxEdit1, cyEdit, hwndOwner, (HMENU) ID_POSITON, Application::hInst, 0 );

	// Set values unique to the band with the combo box.
	GetWindowRect(hwndEditBar, &rc);
	rbBand.lpText     = TEXT("");
	rbBand.hwndChild  = hwndEditBar;
	rbBand.cxMinChild = cxEdit1;
	rbBand.cyMinChild = 26;
	rbBand.cx         = 0;

	// Add the band that has the combo box.
	SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);

	rbBand.fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP | RBBS_NOGRIPPER;

	//----EditBar2---
	hwndEditBar2 = CreateWindowEx(0L, L"Edit", NULL, WS_CHILD | WS_BORDER | ES_READONLY
		| WS_VISIBLE | ES_LEFT | ES_AUTOVSCROLL, 
		cxEdit1+5, 0, 0, cyEdit, hwndOwner, (HMENU) ID_CONTENT, Application::hInst, 0 );

	// Set values unique to the band with the combo box.
	GetWindowRect(hwndEditBar2, &rc);
	rbBand.lpText     = TEXT("");
	rbBand.hwndChild  = hwndEditBar2;
	rbBand.cxMinChild = 100;
	rbBand.cyMinChild = 26;
	rbBand.cx         = 200;

	// Add the band that has the combo box.
	SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);

	return (hwndRB);
}


HWND WINAPI Controls::CreateToolBar(HWND hWndParent)
{
	HWND hWndToolbar;
	int cx_edit = 80, CX_SEPARATOR = 85; // Width of edit control.
	int cy_edit = 25;                // Height of edit control.

	// The code example uses a MACRO defined in the header file as
	// follows: #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))

	// Toolbar buttons
	TBBUTTON tbButtonsCreate [ ] = 
	{		
		{0, ID_FILE_NEW, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{1, ID_FILE_OPEN, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{2, ID_FILE_SAVE, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{3, ID_EDIT_CUT, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{4, ID_EDIT_COPY, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{5, ID_EDIT_PASTE, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{6, ID_EDIT_DELETE, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{7, ID_EDIT_UNDO, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{8, ID_EDIT_REDO, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{22, ID_FONT_SELECT, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{23, ID_FONT_BOLD, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{24, ID_FONT_ITALIC, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{25, ID_FONT_UNDERLINE, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{26, ID_FONT_STRICK, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{16, ID_ALIGN_LEFT, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{17, ID_ALIGN_CENTER, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{18, ID_ALIGN_RIGHT, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{19, ID_VALIGN_TOP, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{20, ID_VALIGN_CENTER, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{21, ID_VALIGN_BOTTOM, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{13, ID_COLOR_TEXT, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{14, ID_COLOR_BACK, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{15, ID_FORMAT_CLEAR, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{11, ID_INSERT_ROW, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{12, ID_INSERT_COLUMN, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{0, 0, TBSTATE_ENABLED, BTNS_SEP, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{9, ID_VIEW_HEADER, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
		{10, ID_VIEW_GRIDLINE, TBSTATE_ENABLED, BTNS_BUTTON, 
#if defined(_WIN32) | defined(_WIN64)
		{0},
#endif
		0L, 0},
	}; 

	hWndToolbar = CreateToolbarEx (hWndParent, 
		WS_CHILD | TBSTYLE_TOOLTIPS | CCS_NORESIZE | TBSTYLE_WRAPABLE | TBSTYLE_FLAT | CCS_NODIVIDER, 
		ID_TOOLBAR, 22+5, Application::hInst,
		IDB_TOOLBAR, tbButtonsCreate, ARRAYSIZE(tbButtonsCreate), 16, 16, 16,
		16, sizeof (TBBUTTON));
	hwndToolBar= hWndToolbar;
	return hWndToolbar;
}

void Controls::SetToolBarButton(int idButton, bool checked)
{
	::SendMessage(hwndToolBar,(UINT)TB_CHECKBUTTON, (WPARAM) idButton,(LPARAM) MAKELONG (checked, 0));
}

void Controls::EnableToolBarButton(int idButton, bool enabled)
{
	::SendMessage(hwndToolBar,(UINT)TB_ENABLEBUTTON, (WPARAM) idButton,(LPARAM) MAKELONG (enabled, 0));
}

void Controls::ReSetToolBar()
{
	SetToolBarButton(ID_FONT_BOLD, false);
	SetToolBarButton(ID_FONT_ITALIC, false);
	SetToolBarButton(ID_FONT_STRICK, false);
	SetToolBarButton(ID_FONT_UNDERLINE, false);
	SetToolBarButton(ID_ALIGN_LEFT, false);
	SetToolBarButton(ID_ALIGN_CENTER, false);
	SetToolBarButton(ID_ALIGN_RIGHT, false);
	SetToolBarButton(ID_VALIGN_TOP, false);
	SetToolBarButton(ID_VALIGN_CENTER, false);
	SetToolBarButton(ID_VALIGN_BOTTOM, false);
}